<?php

class Service_icon extends MY_Service
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('HttpUtil');
        $this->load->config('common/icon_api');
    }

    public function index($domain)
    {
        $site = $this->config->item('favicon');
        $site['url'] .= $domain;
        $options['verify'] = false;
        $res = HttpUtil::cfg_request_options($site, $options);
        $res = json_decode($res, true);
        if (!empty($res) && isset($res['icons'][0]['src'])) {
            return $res['icons'][0]['src'];
        }

        $site = $this->config->item('favicon_api');
        $site['data']['url'] = $domain;
        $site['data']['base64'] = true;
        $options['verify'] = false;
        $res = HttpUtil::cfg_request_options($site, $options);
        $res = json_decode($res, true);
        if (!empty($res['result']['icon'])) {
            return $res['result']['icon'];
        }
        return "";
    }

    public function make($title)
    {
        $text = mb_substr($title, 0, 2);
        $url = "http://api.dreamthere.cn/image/icon?text=$text&width=64&height=64&fontSize=12";
        $res = file_get_contents($url);
        $result = json_decode($res, true);
        return $result['result'] ? : '';
    }

    public function url2base64($url)
    {
        $url = "http://api.dreamthere.cn/image/url2base64?url=$url";
        $res = file_get_contents($url);
        $result = json_decode($res, true);
        return $result['result'] ? : '';
    }

}